import { GasPriceProvider } from "./lib/GasPriceProvider";
import { IERC20, IERC20__factory } from "./contracts/erc20";
import { ethers, Wallet } from "ethers";
import { config } from "./config";

export class Contract {
  provider: ethers.providers.StaticJsonRpcProvider;
  gasProvider: GasPriceProvider;
  tokens: {
    [key: string]: {
      contract: IERC20,
      address: string,
      decimals: number
    }
  } = {};
  signers: Wallet[] = [];

  constructor() {
    this.provider = new ethers.providers.StaticJsonRpcProvider(config.chain.rpc);
    this.gasProvider = new GasPriceProvider(this.provider, config.chain.name, config.chain.gasPrice);
    for (let key of config.privateKeys) {
      const wallet = new Wallet(key, this.provider);
      console.log(`[Contract] Wallet Id: ${this.signers.length} Address: ${wallet.address}`);
      this.signers.push(wallet);
    }
  }

  async init() {
    for (let token of config.tokens) {
      const erc20 = IERC20__factory.connect(token, this.provider);
      const symbol = token === ethers.constants.AddressZero ? "BNB" : await erc20.symbol();
      const decimals = token === ethers.constants.AddressZero ? 18 : await erc20.decimals();

      this.tokens[symbol.toLowerCase()] = {
        contract: erc20,
        address: token,
        decimals: decimals
      };
      console.log(`[Contract] Token Symbol: ${symbol} Address: ${erc20.address} Decimals: ${decimals}`);
    }
  }
}
